import { Button, Form, Input, Space, Table } from 'antd'
import { useRef } from 'react'
import type { TableColumnsType } from 'antd'
import CreateDept from './CreateDept'
import type { IAction } from '@/types/modal'
import deptApi from '@/api/deptApi'
import { useAntdTable } from 'ahooks'
import { formatDate } from '@/utils'
import type { DeptItem, Params } from '@/types/deptType'
import { message, modal } from '@/utils/AntdGlobal'

const DeptList = () => {
  const [form] = Form.useForm()
  const deptRef = useRef<{ open: (type: IAction, data?: DeptItem | { parentId: string }) => void }>(undefined)
  const handleCreate = () => {
    deptRef.current?.open('create')
  }
  const getTableData = ({ current, pageSize }: { current: number; pageSize: number }, formData: Params) => {
    return deptApi.getDeptList(formData).then(res => {
      return {
        total: res.length,
        list: res
      }
    })
  }
  const handleDel = (id: string) => {
    modal.confirm({
      title: '确认删除',
      content: '确认删除该部门吗',
      okText: '确定',
      cancelText: '取消',
      onOk: () => {
        handleSubmitDel(id)
      }
    })
  }
  const handleSubmitDel = async (_id: string) => {
    await deptApi.delDeptById({ _id })
    message.success('删除成功')
    search.submit()
  }
  const handleEdit = (data: DeptItem) => {
    deptRef.current?.open('edit', data)
  }
  const handleSubCreate = (parentId: string) => {
    deptRef.current?.open('create', { parentId })
  }
  const { tableProps, search } = useAntdTable(getTableData, { form })
  const columns: TableColumnsType<DeptItem> = [
    {
      title: '部门名称',
      dataIndex: 'deptName',
      key: 'deptName'
    },
    {
      title: '负责人',
      dataIndex: 'userName',
      key: 'userName'
    },
    {
      title: '更新时间',
      dataIndex: 'updateTime',
      key: 'updateTime',
      render: (value: Date) => {
        return formatDate(value)
      }
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      render: (value: Date) => {
        return formatDate(value)
      }
    },
    {
      title: '操作',
      render: (record: DeptItem) => {
        return (
          <Space>
            <Button type='text' onClick={() => handleSubCreate(record._id)}>
              新增
            </Button>
            <Button type='text' onClick={() => handleEdit(record)}>
              编辑
            </Button>
            <Button type='text' onClick={() => handleDel(record._id)}>
              删除
            </Button>
          </Space>
        )
      }
    }
  ]

  return (
    <div className='dept-list'>
      <Form className='search-form' form={form} layout='inline'>
        <Form.Item label='部门名称' name='deptName'>
          <Input placeholder='请输入部门名称' />
        </Form.Item>
        <Form.Item>
          <Space>
            <Button type='primary' onClick={search.submit}>
              搜索
            </Button>
            <Button onClick={search.reset}>重置</Button>
          </Space>
        </Form.Item>
      </Form>
      <div className='base-table'>
        <div className='header-wrapper'>
          <div className='title'>部门列表</div>
          <div className='action'>
            <Button type='primary' onClick={handleCreate}>
              新增
            </Button>
          </div>
        </div>
        <Table columns={columns} {...tableProps} />
      </div>
      <CreateDept mRef={deptRef} update={search.submit} />
    </div>
  )
}

export default DeptList
